<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CardController extends Controller
{
    /**
     * 显示添加页面
     */
    public function   getAdd()
    {
    	return  view('admin.card.add');    
    }

    /**
     * 插入数据库
     */
    public   function  postInsert(Request $request)
    {
    	//进行表单验证
    	$this->validate($request,[
    		'title' => 'required',
    		'author' => 'required',
    		'time' => 'required',
    		'content' => 'required',
    		'profile' => 'required'
    		],[
    		'title.required' => '标题不能为空',
    		'author.required' => '作者不能为空',
    		'time.required' => '时间不能为空',
    		'content.required' => '内容不能为空',
    		'profile.required' => '文章配图不能为空'
    		]);
    	//获取数据
    	$data = $request->except(['_token']);
    	//处理文章配图
    	if($request->hasFile('profile')){
    		$fileName = date('Ymd',time()).rand(100000,999999);
    		$suffix = $request->file('profile')->getClientOriginalExtension();//获取文件后缀名
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('profile')->move('./Uploads/',$fileName);
    		$data['profile'] = '/Uploads/'.$fileName;
    	}

    	//插入数据库
    	$res = DB::table('card')->insert($data);
    	// 检测
    	if($res){
    		return redirect('/card/index')->with('info','添加成功 ');
    	}else{
    		return back()->with('error','添加失败');
    	}
   
    }

    /**
     * 列表显示
     */

    public function  getIndex(Request $request)
    {
    	//读取数据库
    	$cards = DB::table('card')->where(function($query) use($request){
    		$k = $request->input('keyword');
    		if(!empty($k)){
    			$query->where('content','like','%'.$k.'%');
    		}
    	})->paginate($request->input('num',1));

    	return view('admin.card.index',['cards'=>$cards,'request'=>$request]);
    }

    /**
     * 用户的修改页面
     */
    public function getEdit(Request $request)
    {
    	//读取当前id的用户信息
    	$id = $request->input('id');
    	//读取数据库
    	$cards = DB::table('card')->where('id',$id)->first();
    	//解析模板
    	return  view('admin.card.edit',['cards'=>$cards,'request'=>$request]);
    }

    /**
     * 用户的更新操作
     */

    public function postUpdate(Request $request)
    {
    	//进行表单验证
    	$this->validate($request,[
    		'title' => 'required',
    		'author' => 'required',
    		'time' => 'required',
    		'content' => 'required',
    		'profile' => 'required'
    		],[
    		'title.required' => '标题不能为空',
    		'author.required' => '作者不能为空',
    		'time.required' => '时间不能为空',
    		'content.required' => '内容不能为空',
    		'profile.required' => '文章配图不能为空'
    		]);
    	//图片上传操作
    	$data = $request->except(['_token','id']);
    	//处理上传的文件
    	$path = $this->uploade($request);
    	if(!empty($path)){
    		$data['profile'] = $path;
    	}

    	//更新
    	$res = DB::table('card')->where('id',$request->input('id'))->update($data);
    	if($res){
    		return redirect('/card/index')->with('info','修改成功');
    	}else{
    		return back()->with('error','修改失败');
    	}
    }

    /**
     * 删除操作
     */
    public function getDelete(Request $request)
    {
    	$id = $request->input('id');
    	//执行删除
    	$res = DB::table('card')->where('id',$id)->delete();
    	if($res){
    		return back()->with('info','删除成功');
    	}else{
    		return  back()->with('error','删除失败');
    	}

    }


    /**
     * 封装上传操作代码
     */
    public function uploade($request)
    {
    	if($request->hasFile('profile')){
    		$fileName = date('Ymd',time()).rand(100000,999999);
    		$suffix = $request->file('profile')->getClientOriginalExtension();
    		//获取文件后缀名
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('profile')->move('./Uploads/',$fileName);
    		$data['profile'] = '/Uploads/'.$fileName;
    		return $data['profile'];
    	}
    }


    /**
     * 前台个人多有帖子页面
     */

    public function ad(Request $request)
    {
        //查找该用户的所有历史帖子
        $data = DB::table('card')->where('uid',3)->get();
        //查询帖子分类
        $res = DB::table('session')
               ->join('card','session.id','=','card.pid')
               ->select('session.*','card.*')
               ->get();
        // dd($res);
        //显示个人帖子列表页
        return view('home.card.index',['data'=>$data,'res'=>$res,'request'=>$request]);
    }

}
